package com.leetcode.partition4;

import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2021/8/12 9:41
 */
public class LC322零钱兑换 {

    public static int coinChange(int[] coins, int amount) {
        return dynamicProgramming(coins, amount);
    }

    public static void main(String[] args) {
        int[] coins = {1, 2, 5};
        int amount = 11;
        System.out.println(coinChange(coins, amount));
    }

    private static int dynamicProgramming(int[] coins, int amount) {
        //dp[i]：凑够总额为i所需的钱币最小个数
        int[] dp = new int[amount + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 0; i < coins.length; i++) {
            for (int j = coins[i]; j <= amount; j++) {
                //没有这种情况
                if (dp[j - coins[i]] == Integer.MAX_VALUE) continue;
                dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
            }
        }
        return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
    }
}
